package com.chenxy.common.result;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 公共返回类
 * @author chenxy
 */
@Data
public class Res<T> {
    public final static String SUCCESS = "success";
    public final static String FAIL = "fail";

    /**
     * 代表返回结果的code
     */
    @ApiModelProperty(value = "返回编码")
    private int code;
    /**
     * 代表返回结果的状态 success 和 fail
     */
    @ApiModelProperty(value = "返回状态")
    private String status;

    /**
     * 如果status为success ，则data为前端需要的json数据
     * 如果status为fail，则data为通用的错误码格式
     */
    @ApiModelProperty(value = "返回数据")
    private T data;

    public Res(){

    }

    public Res(int code,String status, T data) {
        this.code = code;
        this.status = status;
        this.data = data;
    }

    public static Res success(Object data){
        return create(0,SUCCESS,data);
    }

    public static Res create(int code,String status, Object data){
        Res res = new Res(code,status,data);
        return res;
    }

    public static Res fail(int errCode, String errMsg) {
        return create(errCode,FAIL,errMsg);
    }
}
